package com.kaishengit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.kaishengit.pojo.Deal;
import com.kaishengit.pojo.DealNote;
import com.kaishengit.pojo.Dto;
import com.kaishengit.pojo.User;
import com.kaishengit.service.DealNoteService;
import com.kaishengit.service.DealService;
import com.kaishengit.util.DateUtil;
import com.opensymphony.xwork2.ActionContext;

public class DealNoteAction {
	private DealNoteService dealNoteService;
	private DealService dealService;
  private String textnote;
  private String dealid;
  private String id;
	public String execute() {
		return "success";
	}
	
	public String del_talk() {
		
		dealNoteService.dels_talk(id);
		return "success";
	}
	
	public String addNote() {

		Map<String, Object>  session=ActionContext.getContext().getSession();
     	User user=(User)session.get("user");
		Deal deal=dealService.findById(dealid);
		DealNote dealnote=new DealNote();
		 dealnote.setContent(textnote);
		 System.out.println(textnote);
		 dealnote.setCreatetime(DateUtil.getNow());
		 dealnote.setDeal(deal);
		 dealnote.setState(deal.getState());
		 
		 dealnote.setUser(user);

	  dealNoteService.save(dealnote);

	  
	  List< DealNote> list= dealNoteService.findAll(deal);
	 List<Dto> lists=new  ArrayList<Dto>();
	  
	  for(DealNote f:list){
	
		  Dto dto =new Dto();
		   dto.setContent(f.getContent());
		   dto.setCreatetime(f.getCreatetime());
		   dto.setId(f.getId());
		   dto.setState(f.getState());
		   dto.setUsername(f.getUser().getUsername());
		    lists.add(dto);
	  }
	  Dto dto =new Dto();
	   dto.setContent(dealnote.getContent());
	   dto.setCreatetime(dealnote.getCreatetime());
	   dto.setId(dealnote.getId());
	   dto.setState(dealnote.getState());
	   dto.setUsername(dealnote.getUser().getUsername());
		 HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=UTF-8");
		
		 Gson gson=new Gson();
		String json= gson.toJson(dto);
	//	System.out.println(json);
	//	System.out.println(json);
		
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(json);
			   out.flush();
			   out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	   
	  
		  
		return null;
		
		
		
	}
	
	
	
	
	
	// get set
	
	
	
	public DealNoteService getDealNoteService() {
		return dealNoteService;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public DealService getDealService() {
		return dealService;
	}

	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}

	

	public String getTextnote() {
		return textnote;
	}

	public void setTextnote(String textnote) {
		this.textnote = textnote;
	}

	public String getDealid() {
		return dealid;
	}

	public void setDealid(String dealid) {
		this.dealid = dealid;
	}

	public void setDealNoteService(DealNoteService dealNoteService) {
		this.dealNoteService = dealNoteService;
	}
	
	
	
}
